﻿using Hospital.IService;
using Hospital.Model.entity;
using Hospital.Utility.ApiResult;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace Hospital.WebApi.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    [Authorize]
    public class WardController(IWardService wardService) : ControllerBase
    {
        [HttpPost]
        public async Task<ActionResult<ApiResult>> PostWard([FromBody] Ward ward)
        {
            bool b = await wardService.CreateAsync(ward);
            return b ? ApiResultHelper.Success(b) : ApiResultHelper.Error(b);
        }
        [HttpGet]
        public async Task<ActionResult<ApiResult>> GetWards()
        {
            try
            {
                var result = await wardService.QueryAsync();
                return ApiResultHelper.Success(result);
            }
            catch (Exception ex)
            {
                return ApiResultHelper.Error(ex);
                throw;
            }
        }
        [HttpPut("{id}")]
        public async Task<ActionResult<ApiResult>> PutWard(int id, [FromBody] Ward ward)
        {
            bool b = await wardService.EditAsync(ward);
            return b ? ApiResultHelper.Success(b) : ApiResultHelper.Error(b);
        }
        [HttpDelete("{id}")]
        public async Task<ActionResult<ApiResult>> DeleteWard(int id)
        {
            bool b = await wardService.SoftDeletion(id);
            return b  ? ApiResultHelper.Success(b) : ApiResultHelper.Error(b);
        }
    }
}
